package light.head.dao.mobile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.Communication;

import org.springframework.stereotype.Service;

/**
 * 通讯录
 * @author yangguanghe @date 2014-6-4 @time 下午4:09:37
 *
 */
@Service
public class MobileCommunicationDAO extends BaseDAO<Communication, Integer> {

	
	
	/**
	 * 查询
	 *@param oc		机构编号
	 *@param map	查询条件
	 *@param pb		分页对象
	 *@author yangguanghe [2014-6-4_下午4:20:18]
	 */
	public RESULT findByMap(Integer oc, Map<String, String> map, PageBean pb) {
		
		if (null == oc) return null;
		
		StringBuffer hql = new StringBuffer("FROM Communication c WHERE c.oc = ?");
		List<Object> parm = new ArrayList<Object>();
		
		parm.add(oc);
		
		if (null != map) {
			String name = map.get(Parm.NAME);
			String st = map.get(Parm.ST);
			String et = map.get(Parm.ET);
			String type = map.get(Parm.TYPE);
			String deleted = map.get(Parm.DELETED);
			
			if (isNotNull(name)) {
				hql.append(" AND c.name LIKE ?");
				parm.add("%" + name + "%");
			}
			
			if (isNotNull(st) && isNotNull(et)) {
				hql.append(" AND c.ct BETWEEN ? AND ?");
				parm.add(DateUtils.getDateByStr(st, "yyyy-MM-dd"));
				parm.add(DateUtils.getDateByStr(et, "yyyy-MM-dd"));
			} else if (isNotNull(st) && !isNotNull(et)) {
				hql.append(" AND c.ct >= ?");
				parm.add(DateUtils.getDateByStr(st, "yyyy-MM-dd"));
			} else if (!isNotNull(st) && isNotNull(et)) {
				hql.append(" AND c.ct <= ?");
				parm.add(DateUtils.getDateByStr(et, "yyyy-MM-dd"));
			}
			
			if (isNotNull(type)) {
				hql.append(" AND c.type = ?");
				parm.add(Byte.parseByte(type));
			}
			
			if (isNotNull(deleted)) {
				hql.append(" AND c.deleted = ?");
				parm.add(Boolean.parseBoolean(deleted));
			}
		}
		
		hql.append(" ORDER BY c.code DESC");
		return findToPageBean(hql.toString(), pb, parm);
	}
	
	
	public RESULT update(Communication c) {
		
		if (null == c || null == c.getCode()) return RESULT.PARAMETER_ERROR;
		
		Communication db = super.get(c.getCode());
		
		if (null == db) return RESULT.NOT_FIND;
		
		boolean ok = false;
		
		if (isNotNull(c.getName()) && (!isNotNull(db.getName()) || !db.getName().equals(c.getName()))) {
			ok = true;
			db.setName(c.getName());
		}
		
		if (isNotNull(c.getEmail()) && (!isNotNull(db.getEmail()) || !db.getEmail().equals(c.getEmail()))) {
			ok = true;
			db.setEmail(c.getEmail());
		}
		
		if (isNotNull(c.getPhone()) && (!isNotNull(db.getPhone()) || !db.getPhone().equals(c.getPhone()))) {
			ok = true;
			db.setPhone(c.getPhone());
		}
		
		if (isNotNull(c.getAddress()) && (!isNotNull(db.getAddress()) || !db.getAddress().equals(c.getAddress()))) {
			ok = true;
			db.setAddress(c.getAddress());
		}
		
		if (null != c.getDeleted() && (null == db.getDeleted() || db.getDeleted().booleanValue() != c.getDeleted().booleanValue())) {
			ok = true;
			db.setDeleted(c.getDeleted());
		}
		
		if (null != c.getBirthday() && (null == db.getBirthday() || db.getBirthday().toString().equals(c.getBirthday().toString()))) {
			ok = true;
			db.setBirthday(c.getBirthday());
		}
		
		if (isNotNull(c.getCompany()) && (!isNotNull(db.getCompany()) || !db.getCompany().equals(c.getCompany()))) {
			ok = true;
			db.setCompany(c.getCompany());
		}
		
		if (isNotNull(c.getRemark()) && (!isNotNull(db.getRemark()) || db.getRemark().equals(c.getRemark()))) {
			ok = true;
			db.setRemark(c.getRemark());
		}
		
		if (null != c.getType() && (null == db.getType() || db.getType().byteValue() != c.getType().byteValue())) {
			ok = true;
			db.setType(c.getType());
		}
		
		if (null != c.getSex() && (null == db.getSex() || db.getSex().booleanValue() != c.getSex().booleanValue())) {
			ok = true;
			db.setSex(c.getSex());
		}
		
		if (ok) {
			return upd(db);
		}
		
		return RESULT.SUCCESS;
		
	}
}
